package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.entity.Product;
import com.situ.mall.entity.Result;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.service.IProductService;

@Service
public class ProductServiceImpl implements IProductService{
	@Autowired
	private ProductMapper productMapper;

	@Override
	public List<Product> selectAll() {
		return productMapper.selectAll();
	}

	@Override
	public Result selectByPage(Integer page, Integer limit, Product product) {
		// 查询一种有多少调数据
		int count = productMapper.getTotalCount(product);
		// 当前页数据
		int offset = (page -1 ) * limit;
		List<Product> list = productMapper.selectByPage(offset, limit, product);
		return Result.success("", list, count);
	}

	@Override
	public boolean deleteAll(Integer[] ids) {
		int count = 0;
		for (Integer id : ids) {
			// 每删除一个影响一行c返回就是1
			int c = productMapper.deleteByPrimaryKey(id);
			count += c;
		}
		
		//int count = productMapper.deleteAll(ids);
		return count == ids.length ? true : false;
	}

	@Override
	public boolean insert(Product product) {
		int count = productMapper.insert(product);
		return count == 1 ? true : false;
	}

	@Override
	public Product selectById(Integer productId) {
		return productMapper.selectByPrimaryKey(productId);
	}

	@Override
	public boolean deleteById(Integer id) {
		int count=productMapper.deleteByPrimaryKey(id);
		return count == 1 ? true : false;
	}

	@Override
	public List<Product> selectProductByCategoryId(Integer categoryId) {
		List<Product> list=productMapper.selectProductByCategoryId(categoryId);
		return list;
	}

}